import { Shortcut_Name_Enum } from "@enum/app.enum";
import { Ipc_ChannelEnum } from "@enum/channel.enum";
import { IpcRender } from "@preload/helper/ipcRender";
import { isFunction } from "@utils/judge";

export class AppShortcutHandle {

  private static shortcutMap: Map<
    Shortcut_Name_Enum,
    Set<(...args: unknown[]) => unknown>
  > = new Map();

  static regist(
    ...params: [Shortcut_Name_Enum, (...args: unknown[]) => unknown]
  ): void {
    IpcRender.send(Ipc_ChannelEnum.APP_REGIST_SHORTCUT, ...params);
    this.setShortcut(...params);
  }

  static unRegist(name: Shortcut_Name_Enum): void {
    IpcRender.send(Ipc_ChannelEnum.APP_UN_REGIST_SHORTCUT, name);
    this.delShortcut(name);
  }

  static setShortcut(
    name: Shortcut_Name_Enum,
    fn: (...args: unknown[]) => unknown
  ): void {
    const shortcuter = this.shortcutMap.get(name);
    if (shortcuter) shortcuter.add(fn);
  }

  static delShortcut(
    name: Shortcut_Name_Enum,
    fn?: (...args: unknown[]) => unknown
  ): void {
    if (isFunction(fn)) {
      const shortcuter = this.shortcutMap.get(name);
      if (shortcuter) shortcuter.delete(fn);
      return;
    }
    this.shortcutMap.delete(name);
  }
}
